from __future__ import with_statement # for reading files using 'with' statement

# Turn the results of a projection -- which show, for each district, the number
# of people of different age groups in each projected year -- into a format that
# ArcGIS can understand. It creates a new file for each variable (e.g. age
# group, number of births, total population etc) that
# shows how its value changes for each district and year. These outputs
# can then be joined to spatial data and transposed.

__author__="nick"
__date__ ="$Apr 24, 2012 9:25:31 AM$"

import sys

if len(sys.argv) != 2:
    print "Usage: python projection_to_csv.py infile.csv"

infile = sys.argv[1]
outfile = infile[:-4]+"_gis.csv"
print "Reading file", infile

with open(infile,'r') as inf:

    linecount = 0 # Count total number of lines
    reg_lc = 24 # Count number of lines read from current region
    region = "" # The name of the region we're reading
    years = [] # Years to transpose
    outfiles = {} # Dictionary to save variable names and associated outfiles

    # Header line (year columns will be appended once they've been read).
    header = "Region,Variable," 

    for line in inf:

        if linecount == 0:
            # First line will be Region, 2004, .., 2100. Find the years to transpose
            linesplit = line.split(',')[1:] # (first item is 'Region', ignore it)
            # Males,Females,People for each year, so only choose every third item
            for a in linesplit[0::3]:
                years.append(a.strip())
                header += ("yr"+a.strip()+",")
            linecount += 1
            header+= "\n"
            continue


        linesplit = line.split(',')

        if reg_lc == 24: # Have read all the data for this region, start a new one
            reg_lc = 0
            region = linesplit[0]
            #print "REGION:",region

        else: # Read some info for this region
            variable = linesplit[0] # Name of the variable (e.g. Age0-4)
            variable = variable.replace("+","pl") # Get rid of '+' symbol
            # Check varialble names don't start with numbers, this confuses the GIS
            try:
                int(variable[0])
                variable = ("a"+variable) # append a character to the variable name
            except ValueError:
                pass # Don't need to do anything, the variable doesn't start with a number

            # Find the output file to write the information about years and districts
            # for this variable
            outf = None
            if linecount<=24: # first time reading variables, need to create files
                outf = open(infile[:-4]+"_"+variable+"_gis.csv",'w')
                outf.write(header)
                outfiles[variable] = outf
            else:
                outf = outfiles[variable]


            # Collect every third value (count for all persons), ignoring the first item (the
            # variable name) and the last one (a newline character)
            values = linesplit[1:-1][0::3] 

            # Write these values
            outf.write(region+","+variable+",")
            for v in values:
                outf.write(v+",")
            outf.write("\n")
            

        reg_lc += 1

        linecount += 1

    # Finished going through input file. Close out files

    for f in outfiles.values():
        f.close()

print "Finished"
